﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows;
using SpectrumAnalyser.ModelObjects;

namespace SpectrumAnalyser.Shell.ViewModels
{
    public class SpectrumFileGroupViewModel:DependencyObject
    {
        public SpectrumFileGroupViewModel()
        {
            ItemsSource = new ObservableCollection<SpectrumFileViewModel>();
        }

        public SpectrumFileGroupViewModel(SpectrumFileGroup fileGroup)
        {
            Id = fileGroup.Id;
            Header = fileGroup.Header;
            ItemsSource = ReferenceEquals(fileGroup.ItemsSource, null) ? 
                new ObservableCollection<SpectrumFileViewModel>(new List<SpectrumFileViewModel>()) 
                : new ObservableCollection<SpectrumFileViewModel>(fileGroup.ItemsSource.Select(x => new SpectrumFileViewModel(x)));
        }

        #region publicProperties
        public Guid Id { get; set; }

        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof (string), typeof (SpectrumFileGroupViewModel), new PropertyMetadata(default(string)));

        public string Header
        {
            get { return (string) GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public ObservableCollection<SpectrumFileViewModel> ItemsSource { get; set; }
        #endregion

        public SpectrumFileGroup ToSpectrumFileGroup()
        {
            return new SpectrumFileGroup
                       {
                           Id = Id,
                           Header = Header,
                           ItemsSource = ReferenceEquals(ItemsSource, null) ? null : ItemsSource.Select(x => x.ToSpectrumFile()).ToList()
                       };
        }
    }
}
